package org.ala.tcp_feign_client.spring.feign.results.impl;

import java.util.concurrent.FutureTask;

import org.ala.tcp_feign_client.spring.feign.results.ITcpFeignClientResultsMapper;
import org.ala.tcp_feign_common.exceptions.SerializableException;
import org.ala.tcp_feign_common.msg.biz_resp.BizRespByteMessage;


/**
 * 同步回调
 * <p>	对同步而言，自己能感知到已经timeout了，并且清除map里的msgId
 * 
 * @author 骆毅(ala)
 * @date 2024年4月23日
 */
public class BizRespResultSyncCallback extends AbstractBizRespResultCallback {
	
	
	/**
	 * 需要等待的BizResp
	 */
	protected Object resp;
	/**
	 * future
	 */
	protected FutureTask<Object> future;
	/**
	 * timeout
	 */
	protected long timeout;
	
	
	public BizRespResultSyncCallback(long msgId, long timeout, ITcpFeignClientResultsMapper resultsMapper) {
		super(msgId, timeout, resultsMapper);
		future = new FutureTask<>(() -> {return resp;});
	}
	
	
	@Override
	public void triggeCallback(BizRespByteMessage msg) throws SerializableException {
		//	给值，通知invoke里等待的线程
		Object resp = buildResp(msg);
		this.setResp(resp);
		this.getFuture().run();
	}
	@Override
	protected void triggeTimeout() {
	}


	public Object getResp() {
		return resp;
	}


	public void setResp(Object resp) {
		this.resp = resp;
	}


	public FutureTask<Object> getFuture() {
		return future;
	}


	public void setFuture(FutureTask<Object> future) {
		this.future = future;
	}


	public long getTimeout() {
		return timeout;
	}


	public void setTimeout(long timeout) {
		this.timeout = timeout;
	}


}
